/**
 * It represents one game of battleships. Each Game has two Players.
 * The Game keeps track of whose turn it is, 
 * and is responsible for determining
 * which Player has won.
 */
public class Game {
	/** 
	 * The height and width of the grid
	 */
	public static final int GRID_SIZE = 10;

	Player player1;
	Player player2;
	Player currentTurn;

	/**
	 * It creates this game's players from the specified names.
	 * Also initializes the game so that player 1 starts first.
	 * 
	 * @param player1Name
	 *            The name of player 1.
	 * @param player2Name
	 *            The name of player 2.
	 */
	public Game(String player1Name, String player2Name) {
		player1 = new Player(player1Name);
		player2 = new Player(player2Name);
		currentTurn = player2;
	}

	/** 
	 * @return Returns player 1. 
	 */
	public Player getPlayer1() {
		return player1;
	}

	/** 
	 * @return Returns player 2. 
	 */
	public Player getPlayer2() {
		return player2;
	}

	/**
	 * Advances the game to the next turn - that is, makes it the next player's
	 * turn. This method will never be called if the Game has already been won,
	 * assuming your whoWon code is correct.
	 */
	public void nextTurn() {
		if (currentTurn == player2) {
			currentTurn = player1;
		} else {
			currentTurn = player2;
		}
	}

	/**
	 * Calculates and returns who has won the game, or returns null if no one
	 * has won the game yet. A player is considered to have won the game when
	 * they have sunk all their opponent's ships.
	 * 
	 * @return The player who won, or null if no one has won yet.
	 */
	public Player whoWon() {
		if (player1.isDefeated()) {
			return player2;
		} else if (player2.isDefeated()) {
			return player1;
		} else {
			return null;
		}
	}

	/**
	 * This method does not change whose turn it currently is, that should be 
	 * This method will never be if the Game has already been
	 * won, assuming whoWon code is correct.
	 * 
	 * @return The player whose turn it is currently.
	 */
	public Player whoseTurn() {
		return currentTurn;
	}
}
